include .env

ARIFLOW_VERSION?=2.1.4
NAMESPACE?=uco-etl
AIRFLOW_CHART_VERSION?=8.5.3
PYTHON_VERSION=3.8
GIT_REPO?=gitlab-registry.f.uco.com/p/it/big/uco-prefect
AIRFLOW_WEBHOOK?=https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=3e4035b5-2a16-457e-9766-34a99ac99347
AIRFLOW_INTEGRATION_WEBHOOK?=https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=3e4035b5-2a16-457e-9766-34a99ac99347
export

.PHONY: init docker_reg install uninstall template

init:
	-kubectl create namespace ${NAMESPACE}
	helm repo add airflow-stable https://airflow-helm.github.io/charts

create_secrets:
	kubectl create secret docker-registry \
		uco-gitlab \
    	--docker-server=gitlab-registry.f.uco.com \
    	--docker-username=${GITLAB_DOCKER_USER} \
    	--docker-password="${GITLAB_DOCKER_PASSWORD}" \
  		-n ${NAMESPACE} --dry-run=client -oyaml | kubectl apply -f -
	kubectl create secret generic \
  		uco-airflow-git-secret \
  		--from-literal=username="${GITLAB_REPO_USER}" \
		--from-literal=password="${GITLAB_REPO_PASSWORD}" \
  		-n ${NAMESPACE} --dry-run=client -oyaml | kubectl apply -f -
	kubectl create secret generic \
		uco-airflow-dbt-secret \
		--from-literal=webhook=${AIRFLOW_WEBHOOK} \
		--from-literal=integration_webhook=${AIRFLOW_INTEGRATION_WEBHOOK} \
		-n ${NAMESPACE} --dry-run=client -oyaml | kubectl apply -f -
	kubectl create secret generic \
		kerberos-keytab \
		--from-file=user.keytab=./secret/user.keytab \
		--from-file=krb5.conf=./secret/krb5.conf \
		-n ${NAMESPACE} -o yaml --dry-run=client | kubectl apply -f -
	kubectl create secret generic \
		flink-operator-secret \
		--from-file=user.keytab=./secret/user.keytab \
		--from-file=krb5.conf=./secret/krb5.conf \
		-n ${NAMESPACE} -o yaml --dry-run=client | kubectl apply -f -
	kubectl create secret generic \
  		uco-airflow-dbt-kyuubi-secret \
  		--from-literal=username="${DBT_MRS_USER}" \
		--from-literal=password="${DBT_MRS_PASSWORD}" \
  		-n ${NAMESPACE} --dry-run=client -oyaml | kubectl apply -f -
	kubectl create secret generic \
  		uco-airflow-dbt-trino-secret \
  		--from-literal=trino_username="${DBT_TRINO_USER}" \
		--from-literal=trino_password="${DBT_TRINO_PASSWORD}" \
  		--from-literal=bi_transfer_username="${DBT_TRINO_BI_TRANSFER_USER}" \
		--from-literal=bi_transfer_password="${DBT_TRINO_BI_TRANSFER_PASSWORD}" \
  		-n ${NAMESPACE} --dry-run=client -oyaml | kubectl apply -f -

install:
	helm upgrade --install -f airflow-value.yaml uco-airflow ../charts/airflow \
	-n ${NAMESPACE} --create-namespace \
	--set airflow.users[0].password="${AIRFLOW_ADMIN_PW}" \
	--set airflow.config.AIRFLOW__WEBSERVER__SECRET_KEY="${AIRFLOW_WEBSERVER_SECRET}" \
	--set airflow.image.repository=${GIT_REPO}/airflow \
	--set airflow.image.tag=${ARIFLOW_VERSION}-python${PYTHON_VERSION} \
	--version ${AIRFLOW_CHART_VERSION}

service_account_auth:
	kubectl create clusterrole airflow-admin --verb=list,get,create,delete,patch,deletecollection --resource=pods,configmaps,secrets,services,persistentvolumeclaims
	kubectl create rolebinding airflow-sa-rolebinding-admin --clusterrole=airflow-admin --serviceaccount=uco-etl:uco-airflow-sa -n ${NAMESPACE}

template:
	-mkdir build
	helm template -f airflow-value.yaml uco-airflow airflow-stable/airflow \
	-n ${NAMESPACE} --create-namespace \
	--set airflow.users[0].password="${AIRFLOW_ADMIN_PW}" \
	--set airflow.config.AIRFLOW__WEBSERVER__SECRET_KEY="${AIRFLOW_WEBSERVER_SECRET}" \
	--set airflow.image.repository=${GIT_REPO}/airflow \
	--set airflow.image.tag=${ARIFLOW_VERSION}-python${PYTHON_VERSION} \
	--version ${AIRFLOW_CHART_VERSION} > ./build/test.yaml

uninstall:
	-helm uninstall uco-airflow -n ${NAMESPACE}